from typing import Optional, Union

from pydantic import BaseModel
from sqlalchemy import ForeignKey
from sqlmodel import SQLModel, BIGINT
from fastapi_amis_admin import amis
from fastapi_amis_admin.models import Field
from ..common import AppBaseModel, PageParams


class SysMenu(AppBaseModel, table=True):
    __tablename__ = 'sys_menu'
    __table_args__ = {'extend_existing': True}
    pid:Optional[int] = Field(index=True, title='上级ID，一级菜单为0')
    top_menu_id:Optional[int] = Field(nullable=False, title='顶级菜单ID',
                                       serialization_alias="topMenuId", validation_alias="topMenuId", alias="topMenuId")
    app_code:Optional[str] = Field( title='所属系统编码',
                                       serialization_alias="appCode", validation_alias="appCode", alias="appCode")
    url:Optional[str] = Field(title='菜单URL')
    permissions:Optional[str] = Field(title='授权', description="(多个用逗号分隔，如：sys:user:list,sys:user:save)")
    type:Optional[int] = Field(title='类型', description="类型 0：菜单   1：按钮")
    icon:Optional[str] = Field(title='菜单图标')
    sort:Optional[int] = Field(title='排序')

class SysLanguage(SQLModel, table=True):
    __tablename__ = 'sys_language'
    __table_args__ = {'extend_existing': True}
    table_name:Optional[str] = Field(primary_key=True, nullable=False, title='表名')
    table_id:Optional[int] = Field(primary_key=True, nullable=False, index=True, title='表主键', foreign_key="sys_menu.id")
    field_name:Optional[str] = Field(primary_key=True, nullable=False, title='字段名')
    field_value:Optional[str] = Field(nullable=False, title='字段值')
    language:Optional[str] = Field(primary_key=True, nullable=False, title='语言')


class SysTopMenu(AppBaseModel, table=True):
    __tablename__ = 'sys_top_menu'
    __table_args__ = {'extend_existing': True}
    id:Optional[int] = Field(primary_key=True, title='主键')
    code:Optional[str] = Field(title='顶部菜单编号')
    name:Optional[str] = Field(title='顶部菜单名')
    source:Optional[str] = Field(title='顶部菜单资源')
    app_code:Optional[str] = Field(title='所属系统编码')
    sort:Optional[int] = Field(title='顶部菜单排序')
    status:Optional[int] = Field(title='状态')



class MenuQueryParams(PageParams):
    name: Optional[str] = Field(title="名称")


class SysMenuDTO(BaseModel):
    name: Union[str, None] = Field(title="菜单名称")
    id: Union[int, None] = Field(default=None, nullable=True, title='id')
    pid: Optional[int] = Field(index=True, title='上级ID，一级菜单为0')
    topMenuId: Optional[int] = Field(nullable=False, title='顶级菜单ID')
    appCode: Optional[str] = Field(title='所属系统编码')
    url: Optional[str] = Field(title='菜单URL')
    permissions: Optional[str] = Field(title='授权', description="(多个用逗号分隔，如：sys:user:list,sys:user:save)")
    type: Optional[int] = Field(title='类型', description="类型 0：菜单   1：按钮")
    icon: Optional[str] = Field(title='菜单图标')
    sort: Optional[int] = Field(title='排序')


    # def __iter__(self):
    #     yield from [(k, v) for (k, v) in self.__dict__.items() if not k.startswith('_')]
